package com.lx.util;//说明:



import javax.net.ssl.SSLSocketFactory;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.math.BigDecimal;
import java.security.MessageDigest;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.*;

/**
 * 创建人:游林夕/2019/3/27 14 53
 */
public final class LX {
    private LX(){}

    /**为空则返回true，不否则返回false*/
    public static boolean isEmpty(Object s){
        if (s instanceof Map){
            return s==null || ((Map)s).size()==0;
        }else if(s instanceof List){
            return s==null || ((List)s).size()==0;
        }else if(s instanceof Set){
            return s==null || ((Set)s).size()==0;
        }else if(s instanceof Object[]){
            return s==null || ((Object[])s).length==0;
        }
        return s==null || "".equals(s) || "null".equals(s);
    }
    /**对象不为null*/
    public static boolean isNotEmpty(Object s){return !isEmpty(s);}
    /**抛异常*/
    public static <T>T exMsg(String msg){throw new RuntimeException(msg);}
    public static <T>T exMsg(Throwable e){throw new RuntimeException(e);}
    public static <T>T exMsg(boolean boo, String msg){
        if (boo) throw new RuntimeException(msg);
        return null;
    }
    /**判断对象为空时抛异常*/
    public static void exObj(Object s){exObj(s,"值不能为空!");}
    /** 判断对象为空时抛异常*/
    public static void exObj(Object s, String exMsg){
        if(isEmpty(s)){
            exMsg(exMsg);
        }
    }
    /**判断map中key对应的value值是否为空*/
    public static void exMap(Map s , String keys){
        exObj(s);
        exObj(keys);
        StringBuilder sb = new StringBuilder();
        for (String object : keys.split(",")) {
            if(isEmpty(s.get(object.trim()))){
                sb.append("没有获取到:"+object);
            }
        }
        if (sb.length()>0){
            exMsg(sb.toString());
        }
    }
    /**获取精确的数字*/
    public static BigDecimal getBigDecimal(Object obj){return new BigDecimal(obj == null?"0":obj.toString());}
    /**比较两个数字的大小*/
    public static int compareTo(Object obj1 , Object obj2){return getBigDecimal(obj1).compareTo(getBigDecimal(obj2));}
    /**获取随机UUID*/
    public static String uuid32(int...ints){return UUID.randomUUID().toString().replace("-","").substring(0,ints.length>0 && ints[0]>0&&ints[0]<32?ints[0]:32);}
    /** 根据时间获取主键id*/
    public static synchronized long primaryid(){return (System.currentTimeMillis()*1000000L)+(System.nanoTime()/100L%1000000L) ;}

    /**将对象转为Map*/
    public static Map<String, Object> toMap(String obj) {return LXJson.toMap(obj);}
    public static <T> T toObj(Class<T> t, String obj){return LXJson.toObj(t,obj);}
    /**将对象转为List*/
    public static List toList(String obj) { return LXJson.toList(obj);}
//    public static <T> List<T> toList(Class<T> t, String obj){
//        List list = LXJson.toList(obj);
//        List<T> ls = new ArrayList<>();
//        if (LX.isNotEmpty(list)){
//            list.forEach(v->{
//                if (t.isAssignableFrom(v.getClass())){
//                    ls.add((T)v);
//                }else{
//                    ls.add(LX.toObj(t,v));
//                }
//            });
//        }
//        return ls;
//    }
    /**将对象转为json串*/
    public static String toJSONString(Object obj) { return LXJson.toJson(obj);}
    /**调用Post请求*/
    public static String doSSLPost(String url, String str, SSLSocketFactory sslSocketFactory){return LXHttp.doSSLPost(url,str,sslSocketFactory);}
    public static String doSSLPost(String url, String str, Map<String, String> property, SSLSocketFactory sslSocketFactory){return LXHttp.doSSLPost(url,str,property,sslSocketFactory);}
    public static String doPost(String url, String str){return LXHttp.doPost(url,str);}
    public static String doPost(String url, String str, Map<String, String> property){return LXHttp.doPost(url,str,property);}
    public static InputStream doPostInputStream(String url, String str, Map<String, String> property){return LXHttp.doPostInputStream(url,str,property);}
    public static String doGet(String url) {return LXHttp.doGet(url,null);}
    public static String doGet(String url, Map<String, String> property) {return LXHttp.doGet(url,property);}
    public static InputStream doGetStream(String url) {return LXHttp.doGetStream(url,null);}

    public static void main(String[]args){
        System.out.println(primaryid());
        System.out.println(Long.toBinaryString(System.currentTimeMillis()).length());
    }
    /*睡眠*/
    public static void sleep(long time){
        try {
            Thread.sleep(time);
        } catch (InterruptedException e) {
            exMsg(e.getMessage());
        }
    }
    /**编码*/
    public static String encode(String str){
        try {
            LX.exObj(str,"需要编码的字符串不能为空!");
            return new String(Base64.getEncoder().encode(str.getBytes("utf-8")));
        } catch (UnsupportedEncodingException e) {
            LX.exMsg(e);
        }
        return null;
    }
    /** 解码*/
    public static String decode(String str){
        try {
            LX.exObj(str,"需要解码的字符串不能为空!");
            byte[] decode = Base64.getDecoder().decode(str);
            LX.exObj(decode,"该编码不正确!");
            return new String(decode,"utf-8");
        } catch (UnsupportedEncodingException e) {
            LX.exMsg(e);
        }
        return null;
    }
    /** 获取md5串*/
    public static String md5(String string){
        try {
            byte[] hash = MessageDigest.getInstance("MD5").digest(string.getBytes("UTF-8"));
            StringBuilder hex = new StringBuilder(hash.length * 2);
            for (byte b : hash) {
                if ((b & 0xFF) < 0x10){
                    hex.append("0");
                }
                hex.append(Integer.toHexString(b & 0xFF));
            }
            return hex.toString();
        }catch (Exception e){
            throw new RuntimeException(e);
        }
    }


    //说明: InputStream -> byte[]
    /**{ ylx } 2021/6/16 21:30 */
    public static byte[] toByteArray(InputStream input){
        try {
            try(ByteArrayOutputStream output = new ByteArrayOutputStream()){
                byte[] buffer = new byte[4096];
                int n = -1;
                while (-1 != (n = input.read(buffer))) {
                    output.write(buffer, 0, n);
                }
                return output.toByteArray();
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    //说明:获取当前格式化日期
    /**{ ylx } 2021/10/27 14:53 */
    public static String getDateFormat(String s) {
        return LocalDateTime.now().format(DateTimeFormatter.ofPattern(s));
    }

    public static class ServiceException extends RuntimeException {
        public ServiceException(Throwable e) {
            super(e);
        }
        public ServiceException(String msg) {
            super(msg);
        }
    }
}









